<!-- 编辑表单 -->
<template>
  <q-card style="width: 600px">
    <!-- 标题 -->
    <q-toolbar>
      <q-toolbar-title>{{ title }}</q-toolbar-title> <q-btn v-close-popup icon="close" round flat />
    </q-toolbar>
    <q-separator />

    <!-- 内容 -->
    <q-card-section>
      <!-- prettier-ignore -->
      <q-form ref="formRef" class="row q-col-gutter-x-md">
        <q-tree-select v-model="dto.buildingId" label="所属楼栋" :rules="[required]" :nodes="treeOptions" node-key="value" class="col-12" />
        <q-input v-model.number="dto.roomFloor" label="所在楼层" :rules="[required]" class="col-12 col-sm-6" type="number" />
        <q-input v-model="dto.roomNo" label="房间编号" :rules="[required]" class="col-12 col-sm-6" />
        <q-select v-model="dto.roomType" label="房间户型" :rules="[required]" :options="ops.roomType" map-options emit-value  class="col-12 col-sm-6" />
        <q-select v-model="dto.roomUsage" label="房间用途" :rules="[required]" :options="ops.roomUsage" map-options emit-value class="col-12 col-sm-6"  />
        <q-input v-model.number="dto.buildArea" label="建筑面积" :rules="[numeric,gt(0)]" class="col-12 col-sm-6"  />
        <q-input v-model.number="dto.usageArea" label="使用面积"  :rules="[numeric,gt(0)]" class="col-12 col-sm-6"  />
        <q-select v-model="dto.roomDeco" label="装修情况" :options="ops.roomDeco" map-options emit-value class="col-12 col-sm-6" />
        <q-select v-model="dto.roomState" label="房间状态" :options="ops.roomState" map-options emit-value class="col-12 col-sm-6" />
        <q-input v-model="dto.remark" label="备注说明" class="col-12 q-mt-md" type="textarea" rows="2" />
      </q-form>
    </q-card-section>
    <q-separator />

    <!-- 操作 -->
    <q-card-actions v-if="!readonly">
      <q-checkbox v-model="closeAfterSave" label="保存后关闭" />
      <q-space />
      <q-btn v-close-popup>取消</q-btn>
      <q-btn color="primary" icon="save" :loading="loading" @click="handleSubmit">保存</q-btn>
    </q-card-actions>
  </q-card>
</template>

<script lang="ts" setup>
  import { gt, numeric, required } from 'src/utils/validator';
  import { queryOptions, useCrudForm } from 'src/composables';

  import crudApi, { RoomDto, getBuildingSelectTree } from 'src/api/community/room';
  import { onMounted, ref } from 'vue';

  // 属性和事件定义
  const props = defineProps({
    id: { type: [String, Number], default: undefined },
    title: { type: String, default: '表单' },
    readonly: { type: Boolean, default: false },
  });
  const emit = defineEmits(['close', 'after-save']);

  // 表单数据
  const initRoom = {} as RoomDto;
  const { formRef, dto, closeAfterSave, loading, handleSubmit } = useCrudForm<RoomDto>(crudApi, initRoom, props, emit);

  const treeOptions = ref<SelectOption[]>([]);
  const treeExpanded = ref<IdType[]>([0]);
  const ops = ref({
    roomState: [] as SelectOption[],
    roomType: [] as SelectOption[],
    roomUsage: [] as SelectOption[],
    roomDeco: [] as SelectOption[],
  });
  onMounted(() => {
    queryOptions('room-dicts').then((data) => {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      ops.value = data as any;
    });
    getBuildingSelectTree().then((data) => {
      treeOptions.value = data;
      data.forEach((t) => {
        treeExpanded.value.push(t.value as IdType);
        t.children?.forEach((c) => treeExpanded.value.push(c.value as IdType));
      });
    });
  });
</script>

<style lang="scss" scoped></style>
